package com.yunquan.ohana.ui.calendar.profiles.add

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityProfilesBinding
import com.yunquan.ohana.entity.RoleEntity
import java.util.Locale

class AddProfilesActivity : BaseActivity<ActivityProfilesBinding, AddProfilesViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_profiles_add
    }

    override fun initVariableId(): Int {
        return BR.profilesViewModel
    }

    override fun initParam() {
        super.initParam()
        Locale.setDefault(Locale("en"))
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.white)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        val bundle = intent.extras
        bundle?.let {
            viewModel!!.mDeviceId = it.getString("deviceId")
            viewModel!!.userType = it.getBoolean("userType")
            val json = it.getString("data")
            if (json != null) {
                viewModel!!.mRole = Gson().fromJson(json, RoleEntity::class.java)
                if (viewModel!!.userType == true) {
                    viewModel!!.isShowDelete.set(true)
                } else {
                    viewModel!!.isShowDelete.set(viewModel!!.mRole?.type == 1)
                }
                viewModel!!.name.set(viewModel!!.mRole?.name)
                viewModel!!.title.set(getString(R.string.edit_profile))
            } else {
                viewModel!!.title.set(getString(R.string.add_profile))
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel!!.ui.observe(this) {
            if (it == 0) {
                setResult(1001)
                finish()
            }
        }
    }
}